//
//  SYNavigationViewController.swift
//  仿微博
//
//  Created by a。 on 16/9/20.
//  Copyright © 2016年 a。. All rights reserved.
//

import UIKit

class SYNavigationViewController: UINavigationController,UIGestureRecognizerDelegate  {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        
        
        print(viewControllers.count)
        if viewControllers.count > 0 {
            
            
            if viewControllers.count == 1{
                //获取上级控制器的titile
                let title = viewControllers.first?.title
                
                viewController.navigationItem.leftBarButtonItem =  UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(backClick))
                
            }else{
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backClick))
                
                
                
            }
            viewController.navigationItem.title = "当前第\(viewControllers.count + 1)级控制器"
        }
        
        
        
        super.pushViewController(viewController, animated: animated)
        
        
    }
    //  是否处理这次开始点击的手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool{
        
        //  如果是根视图控制器的话,不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        
        return true
        
    }
    
    @objc  func backClick()  {
        
        
        popViewController(animated: true)
    }
}
